﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace Home.Haushalt.Lib
{
    /// <summary>
    /// 
    /// </summary>
    public static class LO
    {
        /// <summary></summary>
        public static string XsdContent
        {
            get
            {
                return
                    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                    "<xs:schema attributeFormDefault=\"unqualified\" elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">" +
                    "<xs:include schemaLocation=\"elementtypes.xsd\"/>" +
                    "  <xs:element name=\"Localization\">" +
                    "    <xs:complexType>" +
                    "      <xs:sequence>" +
                    "        <xs:element name=\"Language\">" +
                    "          <xs:complexType>" +
                    "            <xs:sequence>" +
                    "               <xs:choice maxOccurs=\"unbounded\">" +
                    "                   <xs:element name=\"Common\" minOccurs=\"0\" maxOccurs=\"unbounded\"  type=\"LocalizationElement\"/>" +
                    "                   <xs:element name=\"Field\" minOccurs=\"0\" maxOccurs=\"unbounded\"  type=\"LocalizationElement\"/>" +
                    "                   <xs:element name=\"Element\" minOccurs=\"0\" maxOccurs=\"unbounded\"  type=\"LocalizationElement\"/>" +
                    "                   <xs:element name=\"Status\" minOccurs=\"0\" maxOccurs=\"unbounded\"  type=\"LocalizationElement\"/>" +
                    "                   <xs:element name=\"ContextMenu\" minOccurs=\"0\" maxOccurs=\"unbounded\"  type=\"LocalizationElement\"/>" +
                    "                   <xs:element name=\"Message\" minOccurs=\"0\" maxOccurs=\"unbounded\"  type=\"LocalizationElement\"/>" +
                    "               </xs:choice>" +
                    "            </xs:sequence>" +
                    "            <xs:attribute name=\"key\" type=\"xs:string\" use=\"required\" />" +
                    "          </xs:complexType>" +
                    "        </xs:element>" +
                    "      </xs:sequence>" +
                    "    </xs:complexType>" +
                    "  </xs:element>" +
                    "</xs:schema>";
            }
        }

        /// <summary></summary>
        private static XmlDocument _localizationData;

        /// <summary></summary>
        private static string _currentLanguageKey = "de-DE";

        /// <summary>
        /// 
        /// </summary>
        static LO()
        {
            _localizationData = new XmlDocument();
            _localizationData.LoadXml("localizationData", XsdContent);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="part"></param>
        /// <param name="label"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private static string localize(string part, string label, params object[] args)
        {
            string value = "!" + label;

            XmlNode labelNode = _localizationData.SelectSingleNode(
                string.Format("Localization/{0}[@{1}='{2}']/{5}/{3}[@{1}='{4}']", "Language", "key", _currentLanguageKey, "Label", label, part)
                );

            if (labelNode != null)
            {
                value = labelNode.Attributes["value"].Value.Replace("\\n", "\n");

                if (value.Contains("{0}") && args != null && args.Length > 0)
                {
                    value = String.Format(value, args);
                }
            }

            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Common(string label, params object[] args)
        {
            return localize("Common", label, args);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        /// <returns></returns>
        public static string ElementName(Type elementType)
        {
            return localize("Element", string.Format("{0}.Name", elementType.Name));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        /// <returns></returns>
        public static string ElementList(Type elementType)
        {
            return localize("Element", string.Format("{0}.List", elementType.Name));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string GridContextMenu(string key, params object[] args)
        {
            return localize("ContextMenu", key, args);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string Status(string label, params object[] args)
        {
            return localize("Status", label, args);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        /// <param name="columnKey"></param>
        /// <returns></returns>
        public static string ColumnName(Type elementType, string columnKey)
        {
            return localize("Field", string.Format("{0}.{1}", elementType.Name, columnKey));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="elementType"></param>
        /// <param name="columnKey"></param>
        /// <param name="layoutName"></param>
        /// <returns></returns>
        public static string ColumnName(Type elementType, string columnKey, string layoutName)
        {
            return localize("Field", string.Format("{0}.{1}.{2}", elementType.Name, layoutName, columnKey));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="messageLocID"></param>
        /// <param name="boxButtons"></param>
        /// <param name="boxIco"></param>
        /// <param name="messageArgs"></param>
        /// <returns></returns>
        public static DialogResult Message(string messageLocID, MessageBoxButtons boxButtons, MessageBoxIcon boxIco, params object[] messageArgs)
        {
            string message = localize("Message", messageLocID, messageArgs);
            string caption = localize("Message", messageLocID + ".Caption");
            if (caption[0] == '!')
                caption = "";
            return MessageBox.Show(message, caption, boxButtons, boxIco);
        }
    }
}
